﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tisonet.Dobrak.Client.WP8.Services.Projections;
using Tisonet.Dobrak.Domain.Models;

namespace Tisonet.Dobrak.Client.WP8.ViewModels
{
    public class ProjectionsViewModel : ViewModelBase
    {
        private readonly IProjectionsService _service;

        public ProjectionsViewModel(IProjectionsService service)
        {
            _service = service;
            Projections = new ObservableCollection<ProjectionViewModel>();
        }

        public ObservableCollection<ProjectionViewModel> Projections { get; private set; }
        
        public bool IsDataLoaded { get; private set; }

        public void LoadData()
        {
            _service.GetProjectionsAsync(UpdateModel);
        }

        private void UpdateModel(IEnumerable<Projection> projections)
        {
            if (projections != null)
            {
                DateTime today = DateTime.Now;

                foreach (var projection in projections.Where(p => p.Date.DayOfYear >= today.DayOfYear))
                {
                    Projections.Add( new ProjectionViewModel(projection));
                }

                NotifyPropertyChanged("Projections");

                IsDataLoaded = true;
            }
        }

    }
}
